<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Extruded A Light</title>
    <link rel="stylesheet" href="../include/style.css">
  </head>
  <body>
    <header>
      Example from <a href="http://amzn.com/1430236655?tag=html5anim-20"><em>Foundation HTML5 Animation with JavaScript</em></a>
    </header>
    <canvas id="canvas" width="400" height="400"></canvas>
    <aside>Move mouse on canvas element.</aside>

    <script src="../include/utils.js"></script>
    <script src="./classes/point3d.js"></script>
    <script src="./classes/triangle-light.js"></script>
    <script src="./classes/light.js"></script>
    <script>
    window.onload = function () {
      var canvas = document.getElementById('canvas'),
          context = canvas.getContext('2d'),
          mouse = utils.captureMouse(canvas),
          light = new Light(),
          points = [],
          triangles = [],
          fl = 250,
          vpX = canvas.width / 2,
          vpY = canvas.height / 2,
          angleX, angleY;

      //first set
      points[0]  = new Point3d( -50, -250, -50);
      points[1]  = new Point3d(  50, -250, -50);
      points[2]  = new Point3d( 200,  250, -50);
      points[3]  = new Point3d( 100,  250, -50);
      points[4]  = new Point3d(  50,  100, -50);
      points[5]  = new Point3d( -50,  100, -50);
      points[6]  = new Point3d(-100,  250, -50);
      points[7]  = new Point3d(-200,  250, -50);
      points[8]  = new Point3d(   0, -150, -50);
      points[9]  = new Point3d(  50,    0, -50);
      points[10] = new Point3d( -50,    0, -50);
      //second set
      points[11] = new Point3d( -50, -250,  50);
      points[12] = new Point3d(  50, -250,  50);
      points[13] = new Point3d( 200,  250,  50);
      points[14] = new Point3d( 100,  250,  50);
      points[15] = new Point3d(  50,  100,  50);
      points[16] = new Point3d( -50,  100,  50);
      points[17] = new Point3d(-100,  250,  50);
      points[18] = new Point3d(-200,  250,  50);
      points[19] = new Point3d(   0, -150,  50);
      points[20] = new Point3d(  50,    0,  50);
      points[21] = new Point3d( -50,    0,  50);
      
      points.forEach(function (point) {
        point.setVanishingPoint(vpX, vpY);
        point.setCenter(0, 0, 200);
      });

      triangles[0]  = new Triangle(points[0],  points[1],  points[8],  "#cccccc");
      triangles[1]  = new Triangle(points[1],  points[9],  points[8],  "#cccccc");
      triangles[2]  = new Triangle(points[1],  points[2],  points[9],  "#cccccc");
      triangles[3]  = new Triangle(points[2],  points[4],  points[9],  "#cccccc");
      triangles[4]  = new Triangle(points[2],  points[3],  points[4],  "#cccccc");
      triangles[5]  = new Triangle(points[4],  points[5],  points[9],  "#cccccc");
      triangles[6]  = new Triangle(points[9],  points[5],  points[10], "#cccccc");
      triangles[7]  = new Triangle(points[5],  points[6],  points[7],  "#cccccc");
      triangles[8]  = new Triangle(points[5],  points[7],  points[10], "#cccccc");
      triangles[9]  = new Triangle(points[0],  points[10], points[7],  "#cccccc");
      triangles[10] = new Triangle(points[0],  points[8],  points[10], "#cccccc");
      
      triangles[11] = new Triangle(points[11], points[19], points[12], "#cccccc");
      triangles[12] = new Triangle(points[12], points[19], points[20], "#cccccc");
      triangles[13] = new Triangle(points[12], points[20], points[13], "#cccccc");
      triangles[14] = new Triangle(points[13], points[20], points[15], "#cccccc");
      triangles[15] = new Triangle(points[13], points[15], points[14], "#cccccc");
      triangles[16] = new Triangle(points[15], points[20], points[16], "#cccccc");
      triangles[17] = new Triangle(points[20], points[21], points[16], "#cccccc");
      triangles[18] = new Triangle(points[16], points[18], points[17], "#cccccc");
      triangles[19] = new Triangle(points[16], points[21], points[18], "#cccccc");
      triangles[20] = new Triangle(points[11], points[18], points[21], "#cccccc");
      triangles[21] = new Triangle(points[11], points[21], points[19], "#cccccc");
      
      triangles[22] = new Triangle(points[0],  points[11], points[1],  "#cccccc");
      triangles[23] = new Triangle(points[11], points[12], points[1],  "#cccccc");
      triangles[24] = new Triangle(points[1],  points[12], points[2],  "#cccccc");
      triangles[25] = new Triangle(points[12], points[13], points[2],  "#cccccc");
      triangles[26] = new Triangle(points[3],  points[2],  points[14], "#cccccc");
      triangles[27] = new Triangle(points[2],  points[13], points[14], "#cccccc");
      triangles[28] = new Triangle(points[4],  points[3],  points[15], "#cccccc");
      triangles[29] = new Triangle(points[3],  points[14], points[15], "#cccccc");
      triangles[30] = new Triangle(points[5],  points[4],  points[16], "#cccccc");
      triangles[31] = new Triangle(points[4],  points[15], points[16], "#cccccc");
      triangles[32] = new Triangle(points[6],  points[5],  points[17], "#cccccc");
      triangles[33] = new Triangle(points[5],  points[16], points[17], "#cccccc");
      triangles[34] = new Triangle(points[7],  points[6],  points[18], "#cccccc");
      triangles[35] = new Triangle(points[6],  points[17], points[18], "#cccccc");
      triangles[36] = new Triangle(points[0],  points[7],  points[11], "#cccccc");
      triangles[37] = new Triangle(points[7],  points[18], points[11], "#cccccc");
      triangles[38] = new Triangle(points[8],  points[9],  points[19], "#cccccc");
      triangles[39] = new Triangle(points[9],  points[20], points[19], "#cccccc");
      triangles[40] = new Triangle(points[9],  points[10], points[20], "#cccccc");
      triangles[41] = new Triangle(points[10], points[21], points[20], "#cccccc");
      triangles[42] = new Triangle(points[10], points[8],  points[21], "#cccccc");
      triangles[43] = new Triangle(points[8],  points[19], points[21], "#cccccc");

      triangles.forEach(function (triangle) {
        triangle.light = light;
      });

      function move (point) {
        point.rotateX(angleX);
        point.rotateY(angleY);
      }

      function depth (a, b) {
        return (b.getDepth() - a.getDepth());
      }

      function draw (triangle) {
        triangle.draw(context);
      }

      (function drawFrame () {
        window.requestAnimationFrame(drawFrame, canvas);
        context.clearRect(0, 0, canvas.width, canvas.height);
        
        angleX = (mouse.y - vpY) * 0.0005;
        angleY = (mouse.x - vpX) * 0.0005;
        points.forEach(move);
        triangles.sort(depth);
        triangles.forEach(draw);
      }());
    };
    </script>
  </body>
</html>
